package com.example.springboot.controller;

import com.example.springboot.pojo.User;
import com.example.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/zxr")
    public String index() {
        return "登录";
    }

    @RequestMapping("/shouye")
    public String index1() {
        return "首页";
    }

    @RequestMapping("/Login")
    public String login() {
        return "注册";
    }

    @RequestMapping("/ys")
    public String login1(String text1, String text, String password, Model model) {
        String s = null;
        int num = userService.addUser(new User(text1, text, password));
        if (num > 0) {
            s = "你已注册成功，将在3s后自动跳转到登录界面进行登录。";
        }
        model.addAttribute("msg", s);
        return "注册成功";
    }

    @RequestMapping("/menu")
    public String menu(String text, String password, Model model) {
        User un = userService.getUserById(text);

        if (text.equals(un.getUsername()) && password.equals(un.getPassword())) {
            String s = un.getName();
            model.addAttribute("msg",s);
            return "首页";
        } else {
            return "登录失败";
        }
    }

    @RequestMapping("/update")
    public String update(String text1, String text, String password, Model model){
    User un = userService.getUserById(text);
    String s = null;
        if(text1.equals(un.getName()) && text.equals(un.getUsername())){
            int user = userService.updateUser(new User(un.getName(),un.getUsername(),password));
            if(user > 0)
                s = "修改成功，将在1s后返回登录界面";
            else
                s = "修改密码失败";
        }
        else{
            s = "不存在此用户或者账号密码不一致";
        }
        model.addAttribute("msg",s);
    return "修改成功";
    }
    @RequestMapping("/test")
    public String cv(Model model) {
        List<User> userList = userService.getUserInfo();
        model.addAttribute("msg", userList);
        return "111";
    }
    @RequestMapping("/exit")
    public String exit(){
        return "登录";
    }
}
